<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\cms\admin\model;

use think\Model;
use app\index\addons\Url;
use app\admin\model\Config;
use app\admin\model\Catalog;

class Address extends Model
{
    protected $name = 'cms_address';

    // 设置json类型字段
    protected $json = ['field'];

    // 设置JSON数据返回数组
    protected $jsonAssoc = true;

    // 搜索器
    public function searchCatalogAttr($query, $value, $array)
    {
        if (! empty($value)) {
            $query->where("catalog_id", "find in set", $value);
        }
    }

    public function searchKeywordAttr($query, $value, $array)
    {
        if (! empty($value)) {
            $query->where("title|address|nickname|phone",'like', '%' . $value . '%');
        }
    }

    public function searchStatusAttr($query, $value, $array)
    {
        if ($value !== '') { 
            $query->where('status', $value);
        }
    }
    
    public function searchAttrAttr($query, $value, $array)
    {
        if ($value !== '' && $value !== null) { 
            $query->where($value, 1);
        }
    }
    
    // 获取器
    public function getUrlAttr($value, $array)
    {
        return Url::single($array);
    }

    public function getCMapAttr($value, $array)
    {
        return $array['address'];
    }

    public function getMapAttr($value, $array)
    {
        return ['address' => $array['address'], 'latitude' => $array['latitude'], 'longitude' => $array['longitude']];
    }

    public function getCatalogIdAttr($value, $array)
    {
        return $value ? array_map('intval', explode(',', $value)) : [];
    }

    public function getFieldAttr($value, $array)
    {
        $field = [];
        $config = Config::getVal('app_cms'); // 配置设置
        if (isset($config['address_field'])) {
            $field  = $config['address_field'];
            foreach ($field as $k => $v) {
                $field[$k]['type']['value'] = isset($value[$v['field']]) ? $value[$v['field']] : $v['type']['value'];
            }
        }
        return $field;
    }
    
    public function getAttrAttr($value, $array)
    {
        $arr = [];
        if ($array['istop'] === 1) {
            array_push($arr, '置顶');
        }
        if ($array['isrecommend'] === 1) {
            array_push($arr, '推荐');
        }
        if ($array['ishot'] === 1) {
            array_push($arr, '热门');
        }
        return $arr;
    }

    // 修改器
    public function setSeoDescriptionAttr($value, $array)
    {
        return empty($value) ? csubstr($array['content']) : $value;
    }

    public function setSeoTitleAttr($value, $array)
    {
        return empty($value) ? $array['title'] : $value;
    }

    public function setSeoKeywordsAttr($value, $array)
    {
        return empty($value) ? $array['title'] : $value;
    }

    public function setFieldAttr($value, $array)
    {
        $field = [];
        foreach ($value as $key => $val) {
            $field[$val['field']] = $val['type']['value'];
        }
        return $field;
    }

    public function setCatalogIdAttr($value, $array)
    {
        return implode(',', $value);
    }
}